<?php
/* Web Statistics and Analytics Console (PHP)
 *  subpages/generate_data/clear.php
 *  Author: Trevor Bidyk
 *  Created: January 1, 2000
 *  Last modified: April 4, 2011
 *   (C) Us 2011
 *   Uses Simple PHP Framework, licensed under the FreeBSD license.
 *    View license in ./include/LICENSE.
 *   All rights reserved.
 */
if (isset($_POST['CancelButton']) && $_POST['CancelButton']) {
    redirect('./generate_data.php?do=generate');
    exit;
}

$db = Database::getDatabase();
$Temp = array();

// Get the number of items we currently have in the database (so we can crush them).
$Result = $db->query('SELECT COUNT(Event_ID) AS Count FROM The_Event');
if ($db->hasRows($Result)) {
    $Temp = $db->getRow();
    $ExistingEventCount = $Temp['Count'];
}
$Result = $db->query('SELECT COUNT(HTML_Sender_ID) AS Count FROM HTML_Sender');
if ($db->hasRows($Result)) {
    $Temp = $db->getRow();
    $ExistingSenderCount = $Temp['Count'];
}
$Result = $db->query('SELECT COUNT(Session_ID) AS Count FROM Event_Session');
if ($db->hasRows($Result)) {
    $Temp = $db->getRow();
    $ExistingSessionCount = $Temp['Count'];
}

// Let's process this stuff now, baby.
if (isset($_POST['SubmitButton']) && $_POST['SubmitButton']) {
    $AffectedEvents = $AffectedEventSessions = $AffectedUserSessions = 0;
    $Result;

    // Clear everything event related.
    if (isset($_POST['ClearEvents']) && $_POST['ClearEvents']) {
        $Result = $db->query('DELETE FROM The_Event');
        $AffectedEvents = $db->affectedRows($Result);
        $Result = $db->query('DELETE FROM HTML_Sender');
        $AffectedSenders = $db->affectedRows($Result);
        $Result = $db->query('DELETE FROM Event_Session');
        $AffectedEventSessions = $db->affectedRows($Result);
    }

    // Clear event types.
    if (isset($_POST['ClearEventTypes']) && $_POST['ClearEventTypes']) {
        $Result = $db->query('DELETE FROM Event_Type');
        $AffectedEventTypes = $db->affectedRows($Result);
    }

    // Clear user sessions
    if (isset($_POST['ClearUserSessions']) && $_POST['ClearUserSessions']) {
        $Result = $db->query('DELETE FROM sessions');
        $AffectedUserSessions = $db->affectedRows($Result);
        $Auth->logout();
    }
}
DisplayHeader('generate', 'clear');
?>
<div style="margin: 20px">
    <?php
    if (isset($_POST['SubmitButton']) && $_POST['SubmitButton']) {
        echo "\t<div id=\"statspanel\">";
        echo "\t\t<h4>Operation Result:</h4>";
        echo "\t\t<span>Events removed: $AffectedEvents</span><br />";
        echo "\t\t<span>Senders removed: $AffectedSenders</span><br />";
        echo "\t\t<span>Event sessions removed: $AffectedEventSessions</span><br />";
        echo "\t\t<br />";
        echo "\t\t<span>Data types removed: $AffectedEventTypes</span><br />";
        echo "\t\t<br />";
        echo "\t\t<span>Sessions removed: $AffectedUserSessions</span><br />";
        echo "\t</div>";
    }
    ?>
    <h2>Really clear the entire database?</h2>
    <span>Events, Senders and Sessions will be all cleared at once to maintain data integrity. Clearing data types is also supported, but is considered an awful idea. Clearing user sessions will log you out of your current session in console.</span><br />
    <br />
    <form action="./generate_data.php?do=clear" method="post">
        <input type="checkbox" id="ClearEvents" name="ClearEvents" value="ClearEvents" checked="checked" />
        <label for="ClearEvents">Clear Events: </label><br />
        <em><?php echo $ExistingEventCount ?> events will be removed.</em><br />
        <em><?php echo $ExistingSenderCount ?> senders will be removed.</em><br />
        <em><?php echo $ExistingSessionCount ?> event sessions will be removed.</em><br />
        <br />
        <input type="checkbox" id="ClearEventTypes" name="ClearEventTypes" value="ClearEventTypes" />
        <label for="ClearEventTypes">Clear data types: </label><br />
        <em>Clearing event types will make inserting new events impossible, due to data integrity rules.</em><br />
        <br />
        <input type="checkbox" id="ClearUserSessions" name="ClearUserSessions" value="ClearUserSessions" />
        <label for="ClearUserSessions">Clear user sessions: </label><br />
        <em>You will be logged out and have to the log in again.</em><br />
        <br />
        <input type="submit" name="SubmitButton" value="Submit" />
        <input type="submit" name="CancelButton" value="Hell, no!" />
    </form>
</div>
